const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');

const resolve = dir => {
	return path.join(__dirname, dir);
};
const devServerProxyFun = urls => {
	// const proxyTarget = 'http://www.yifaxueyuan.com';
	const proxyTarget = 'http://www.yjzgo.com';
	// const proxyTarget = 'http://10.159.62.215';

	const devServerProxy = {};
	urls.forEach(url => {
		devServerProxy[url] = {
			target: proxyTarget,
			pathRewrite: {
				[`^${url}`]: `${url}`,
			},
		};
	});
	return devServerProxy;
};
module.exports = {
	// css相关配置
	css: {
		// css预设器配置项
		loaderOptions: {
			scss: {
				// 默认加载全局变量文件 index.scss
				prependData: `@charset "UTF-8"; @import "~src/uni.scss"; @import "~scss/helpers/index.scss";`,
			},
		},
	},
	devServer: {
		port: 8000,
		host: '0.0.0.0',
	},
	chainWebpack: config => {
		// 重新设置 alias
		config.resolve.alias
			// 静态资源地址
			.set('@', resolve('src'))
			// scss 资源地址
			.set('scss', resolve('src/scss'))
			.set('utils', resolve('src/utils'))
			.set('components', resolve('src/components'))
			.set('pages', resolve('src/pages'))
			.set('api', resolve('src/api'))
			.set('src', resolve('src'));
	},
	// eslint-disable-next-line no-dupe-keys
	configureWebpack: config => {
		config.plugins.push(
			new CopyWebpackPlugin([
				{
					from: path.join(__dirname, '/static'),
					to: path.join(
						__dirname + '/unpackage/',
						'dist',
						process.env.NODE_ENV === 'production' ? 'build' : 'dev',
						process.env.UNI_PLATFORM,
						'/'
					),
				},
			])
		);
	},
};
